
#ifndef __DRIVER_ST7701_RGB_H__
#define __DRIVER_ST7701_RGB_H__

#include "fr30xx.h"
#include "app_config.h"
#include "app_lvgl.h"

#define  RGB_ROW         LV_HOR_RES_MAX //480
#define  RGB_COLUMN      LV_VER_RES_MAX //480

/* Exported functions --------------------------------------------------------*/

typedef struct
{
    uint32_t VerticalSignalCount;

    unsigned char *rgb_TxData;

    uint32_t vs_tvw_count;
    uint32_t vs_back_count;
    uint32_t vs_front_count;

}struct_RGB_TypeDef_t;

#define RGB565_LCD_DCLK_PORT				GPIOD
#define RGB565_LCD_DCLK_GPIO				GPIO_PIN_9

#define RGB565_LCD_VSYNC_PORT				GPIOD
#define RGB565_LCD_VSYNC_GPIO				GPIO_PIN_8

#define RGB565_LCD_HSYNC_PORT				GPIOD
#define RGB565_LCD_HSYNC_GPIO				GPIO_PIN_11

#define RGB565_LCD_DISP_PORT				GPIOD
#define RGB565_LCD_DISP_GPIO				GPIO_PIN_10

#define RGB565_LCD_DE_EN_PORT				GPIOD
#define RGB565_LCD_DE_EN_GPIO				GPIO_PIN_15

#define RGB565_LCD_DATA_PORT				GPIOC
#define RGB565_LCD_DATA_GPIO				0xFFFF //GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7

#define RGB565_LCD_RESET_PORT				GPIOD
#define RGB565_LCD_RESET_GPIO				GPIO_PIN_10

#define RGB565_LCD_BACKLIGHT_PORT 	  		GPIOC   //GPIOB 
#define RGB565_LCD_BACKLIGHT_GPIO 			GPIO_PIN_6  //GPIO_PIN_7  //GPIO_PIN_14


#ifdef  RGB565_LCD_TE_EN
#define RGB565_LCD_TE_PORT 	  				GPIO_B
#define RGB565_LCD_TE_GPIO 					GPIO_PIN_7
#endif

#define RGB56_LCD_INIT_CONFIG					

#ifdef RGB56_LCD_INIT_CONFIG
#define RGB565_LCD_SPI_CS_PORT 	  			GPIOA 
#define RGB565_LCD_SPI_CS_GPIO 				GPIO_PIN_4

#define RGB565_LCD_SPI_CLK_PORT 	  		GPIOA 
#define RGB565_LCD_SPI_CLK_GPIO 			GPIO_PIN_5

#define RGB565_LCD_SPI_MOSI_PORT 	  		GPIOA 
#define RGB565_LCD_SPI_MOSI_GPIO 			GPIO_PIN_6

#define RGB565_LCD_SPI_MISO_PORT 	  		GPIOA
#define RGB565_LCD_SPI_MISO_GPIO 			GPIO_PIN_7

#define RGB565_LCD_SPI_SEL       			SPIM0
	
#define rgb_spi_cs_set()		 	 	 gpio_write_pin(RGB565_LCD_SPI_CS_PORT,RGB565_LCD_SPI_CS_GPIO,GPIO_PIN_CLEAR)	
#define rgb_spi_cs_release()		 	 gpio_write_pin(RGB565_LCD_SPI_CS_PORT,RGB565_LCD_SPI_CS_GPIO,GPIO_PIN_SET) 	

#define rgb_spi_clk_set()		 	 	 gpio_write_pin(RGB565_LCD_SPI_CLK_PORT,RGB565_LCD_SPI_CLK_GPIO,GPIO_PIN_SET) 	
#define rgb_spi_clk_clear()		 	     gpio_write_pin(RGB565_LCD_SPI_CLK_PORT,RGB565_LCD_SPI_CLK_GPIO,GPIO_PIN_CLEAR)	

#define rgb_spi_mosi_set()		 	 	 gpio_write_pin(RGB565_LCD_SPI_MOSI_PORT,RGB565_LCD_SPI_MOSI_GPIO,GPIO_PIN_SET) 	
#define rgb_spi_mosi_clear()		 	 gpio_write_pin(RGB565_LCD_SPI_MOSI_PORT,RGB565_LCD_SPI_MOSI_GPIO,GPIO_PIN_CLEAR)	


#endif

/* signal drive*/
#define rgb_lcd_enable_set()		 	 gpio_write_pin(RGB565_LCD_DE_EN_PORT,RGB565_LCD_DE_EN_GPIO,GPIO_PIN_SET)	
#define rgb_lcd_enable_release()		 gpio_write_pin(RGB565_LCD_DE_EN_PORT,RGB565_LCD_DE_EN_GPIO,GPIO_PIN_CLEAR)	

#define rgb_lcd_vsync_set()		 	 	 gpio_write_pin(RGB565_LCD_VSYNC_PORT,RGB565_LCD_VSYNC_GPIO,GPIO_PIN_SET) 	
#define rgb_lcd_vsync_release()		 	 gpio_write_pin(RGB565_LCD_VSYNC_PORT,RGB565_LCD_VSYNC_GPIO,GPIO_PIN_CLEAR)	

#define rgb_lcd_hsync_set()		 	 	 gpio_write_pin(RGB565_LCD_HSYNC_PORT,RGB565_LCD_HSYNC_GPIO,GPIO_PIN_SET) 	
#define rgb_lcd_hsync_release()			 gpio_write_pin(RGB565_LCD_HSYNC_PORT,RGB565_LCD_HSYNC_GPIO,GPIO_PIN_CLEAR)	

#define rgb_lcd_reset_set()		 	 	 gpio_write_pin(RGB565_LCD_RESET_PORT,RGB565_LCD_RESET_GPIO,GPIO_PIN_SET) 	
#define rgb_lcd_reset_release()		 	 gpio_write_pin(RGB565_LCD_RESET_PORT,RGB565_LCD_RESET_GPIO,GPIO_PIN_CLEAR)	

#define rgb_lcd_disp_set()		 	 	 gpio_write_pin(RGB565_LCD_DISP_PORT,RGB565_LCD_DISP_GPIO,GPIO_PIN_SET) 	
#define rgb_lcd_disp_release()		 	 gpio_write_pin(RGB565_LCD_DISP_PORT,RGB565_LCD_DISP_GPIO,GPIO_PIN_CLEAR)	

#define rgb_lcd_backlight_set()		 	 gpio_write_pin(RGB565_LCD_BACKLIGHT_PORT,RGB565_LCD_BACKLIGHT_GPIO,GPIO_PIN_SET) 	
#define rgb_lcd_backlight_release()		 gpio_write_pin(RGB565_LCD_BACKLIGHT_PORT,RGB565_LCD_BACKLIGHT_GPIO,GPIO_PIN_CLEAR)	




#define __RGB_LCD_DENABLE_SET                          rgb_lcd_enable_release  
#define __RGB_LCD_DENABLE_RELEASE                      rgb_lcd_enable_set 

#define __RGB_LCD_VSYNC_SET                             rgb_lcd_vsync_set
#define __RGB_LCD_VSYNC__RELEASE                        rgb_lcd_vsync_release
                                                            
#define __RGB_LCD_HSYNC_SET                             rgb_lcd_hsync_set
#define __RGB_LCD_HSYNC__RELEASE                        rgb_lcd_hsync_release 
                                                            
#define __RGB_LCD_RESET_SET                             rgb_lcd_reset_set
#define __RGB_LCD_RESET_RELEASE                         rgb_lcd_reset_release 
                                                            
#define __RGB_SPI_CS_RELEASE                            rgb_spi_cs_release
#define __RGB_SPI_CS_SET                                rgb_spi_cs_set

#define __RGB_SPI_CLK_CLEAR                             rgb_spi_clk_clear
#define __RGB_SPI_CLK_SET                               rgb_spi_clk_set

#define __RGB_SPI_MOSI_CLEAR                             rgb_spi_mosi_clear
#define __RGB_SPI_MOSI_SET                               rgb_spi_mosi_set


#define __8080_DATA_WR_LEN(__LEVEL__)                       __PARALLEL_SET_WR_LEN(hparallel.PARALLELx,__LEVEL__)
#define __8080_TXFIFO_EMPTY()                               __PARALLEL_INT_STATUS(hparallel.PARALLELx)&INT_TXFIFO_EMPTY
#define __8080_WRITE_BLANK()                                hparallel.PARALLELx->TX_FIFO = 0

#define __TIMER_CLEAR_IQR(__TIMERx__)                       timer_int_clear(__TIMERx__)
#define __TIMER_INIT(__TIMERx__, __LoadCount__)             timer_init(__TIMERx__, (24000 * __LoadCount__))
#define __TIMER_INT_ENABLE(__TIMERx__)                      timer_int_enable(__TIMERx__)
#define __TIMER_START(__TIMERx__)                           timer_start(__TIMERx__)

#define __DMA_GET_TFR_STATUS()                              dma_get_tfr_Status(&dma_handle)
#define __DMA_CLEAR_TFR_STATUS()                            dma_clear_tfr_Status(&dma_handle)
#define __DMA_TO_8080_START_IT(__BUFFER__,__SIZE__)         dma_start_IT(&dma_handle, (uint32_t)__BUFFER__, (uint32_t)&hparallel.PARALLELx->TX_FIFO, __SIZE__)  

#define __SPI_WRITE_DATA(__BUFFER__, __SIZE__)              spi_master_transmit_X1(&spi_handle, (void *)__BUFFER__, __SIZE__)


/* Exported functions --------------------------------------------------------*/

/* rgb_display_start */
/* rgb_timer_IRQHandler */
/* rgb_dma_IRQHandler */
void rgb_display_start(struct_Timer_t *TIMERx, struct_RGB_TypeDef_t *hrgb, uint32_t fps, unsigned char *Imagedata);
void rgb_timer_IRQHandler(struct_Timer_t *TIMERx, struct_RGB_TypeDef_t *hrgb);
void rgb_dma_IRQHandler(struct_RGB_TypeDef_t *hrgb);

void st7701_init(void* buffer);
void st7701_rgb_display_dma_irq(void);

__RAM_CODE void rgb_display_dma_irq(void);


#endif
